/*
 * @Description: 定时器类，传入函数，定时执行函数
 * @Author: luosilei
 * @Date: 2023-11-28 15:32:20
 * @LastEditTime: 2023-12-19 17:54:30
 * @LastEditors: luosilei
 */

export default class Timer {
    constructor(callback, interval = 1000) {
        this.timerId = null;
        this.elapsedSeconds = 0;
        this.timerEnable = false;
        this.callback = callback;
        this.interval = interval;
    }
    start() {
        this.timerEnable = true;
        if (typeof this.callback === "function") {
            this.callback(this.elapsedSeconds);
        }
        this.timerId = setInterval(() => {
            this.elapsedSeconds++;
            if (typeof this.callback === "function") {
                this.callback(this.elapsedSeconds);
            }
        }, this.interval);
    }
    pause() {
        this.timerEnable = false;
        clearInterval(this.timerId);
        this.timerId = null;
    }
    status() {
        return this.timerEnable;
    }
}
